package com.yizhubao.app.app;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.weikaiyun.fragmentation.SupportActivity;
import com.yizhubao.app.utils.Constants;
import com.yizhubao.app.utils.Utils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import h.framework.pacy.P;
import h.framework.pacy.utils.LogUtil;

/**
 * @author PACY
 * 创建时间 21/6/4 9:26.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述 application入口
 */
public class YZBApp extends Application {
    /**
     * 是否打印调试LOG
     */
    public boolean isDebug=false;

    private List<SupportActivity> activityList = new LinkedList<SupportActivity>();
    private List<SupportActivity> activityList2 = new LinkedList<SupportActivity>();
    private static YZBApp instance;

    //微信Api
    public static IWXAPI api;
    @Override
    public void onCreate() {
        super.onCreate();
        P.init(this);
        //开起LOG 上线时关闭
        P.setLogDebug(isDebug);
        //设置LOG  TAG
        P.setLogTag(Constants.TAG);
        //设置APPNAME 用于SP的存储等
        P.setAPPName(Constants.NAME);
        if(!Utils.isFirst()){
            regToWx();
        }
    }

    public static YZBApp getInstance() {
        if(null == instance) {
            instance = new YZBApp();
        }
        return instance;
    }

    //添加Activity到容器中
    public void addActivity(SupportActivity activity)  {
        activityList.add(activity);
    }

    //遍历所有Activity并finish
    public void exit() {
        for(SupportActivity activity:activityList) {
            activity.finish();
        }
        activityList.clear();
    }

    //添加Activity到容器中
    public void addWelcomeActivity(SupportActivity activity)  {
        activityList2.add(activity);
    }

    public void removeWelcome(){
        if(activityList2.size()>0){
            activityList2.get(0).finish();
        }
    }

    /**
     * 注册微信
     */
    private void regToWx(){
        LogUtil.i("微信注册");
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, Constants.WX_APP_ID, true);
        // 将应用的appId注册到微信
        api.registerApp(Constants.WX_APP_ID);

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // 将应用的appId注册到微信
                api.registerApp(Constants.WX_APP_ID);
            }
        },new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
    }


}
